package cn.le.container;

import java.util.HashSet;

/**
 *
 * HashSet是利用对象equals和hashCode来区别对象是否相同来维护保存对象的单一性,
 * 所以要求保存的对象重写equals和hashCode方法,如果没有重写将调用object默认的equals和hashCode方法,
 * 默认的equals方法对比是引用是否相同
 *
 */
public class HashSetCS {

    public static void main(String[] args) {

        HashSet<A> set = new HashSet<A>();

        set.add(new A("1",2));
        set.add(new A("1",2));

        System.out.println(set.size());

    }
}
class A{

    String name;
    int age;

    public A(String name,int age)
    {
        this.name=name;
        this.age=age;
    }

   /* @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        A a = (A) o;

        if (age != a.age) return false;
        return name != null ? name.equals(a.name) : a.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }*/
}